<template>
  <div class="main">
    <div class="filter-box">
      <div class="filter-left"></div>
      <div class="filter-right">
        <el-button type="primary" size="mini" icon="el-icon-plus" @click="handleOpenAddDialog">添加</el-button>
        <el-button type="primary" size="mini" icon="el-icon-delete" @click="handDeleteAll">删除</el-button>
      </div>
    </div>
    <!-- 表格 -->
    <el-table
        stripe
        height="100%"
        :data="userList"
        tooltip-effect="dark"
        align="center"
        size="mini"
        @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" fixed="left"></el-table-column>
      <el-table-column label="名称" prop="name" width="100" show-overflow-tooltip></el-table-column>
      <el-table-column label="部门" prop="deptName" min-width="120" show-overflow-tooltip></el-table-column>
      <el-table-column label="区域" prop="regionName" min-width="120" show-overflow-tooltip></el-table-column>
      <el-table-column label="所属标签" prop="tagName" width="100" show-overflow-tooltip></el-table-column>
      <el-table-column prop="remark" label="备注" show-overflow-tooltip></el-table-column>
    </el-table>
    <!-- 添加/编辑员工  -->
    <el-dialog v-el-drag-dialog custom-class="custom-dialog" :visible.sync="dialog" title="添加人员"
               width="800px " append-to-body>
      <div style="height: 500px">
        <AddItem v-if="dialog" @save="onSave"/>
      </div>
    </el-dialog>
  </div>
</template>
<script>
// components
import AddItem from './userList'

export default {
  components: {
    AddItem
  },
  props: {
    userList:Array
  },
  data() {
    return {
      selectOptions: [],
      //弹窗
      dialog: false,
    }
  },
  methods: {
    handleOpenAddDialog() {
      this.dialog = true
    },
    //全选
    handleSelectionChange(val) {
      this.selectOptions = val
    },
    // 删除
    handDeleteAll() {
      if (!this.selectOptions.length) return this.$msgPrompt.warning()
      this.$confirm(this.$confirmMsg, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
          .then(() => {
            for (let i = 0; i < this.selectOptions.length; i++) {
              for (let j = 0; j < this.userList.length; j++) {
                if (this.selectOptions[i].id === this.userList[j].id) {
                  this.userList.splice(i,1)
                }
              }
            }
          })
          .catch(() => {
            this.$msgPrompt.info()
          })
    },
    onSave(item) {
      if (this.userList.every(i => i.id !== item.id)) {
        this.userList.push(item)
      }
      this.dialog = false
      this.$msgPrompt.success()
    }
  }
}
</script>
<style scoped lang="scss">
.main {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 10px 10px 0 10px;

  .el-select {
    width: 200px;
  }
}
</style>
